//
//  BK_Confirm_Order_ChooseExpress_Cell_Header.m
//  bestkeep
//
//  Created by TOPTEAM on 2017/2/21.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BK_Confirm_Order_ChooseExpress_Cell_Header.h"
#import "BK_OD_Color.h"
@implementation BK_Confirm_Order_ChooseExpress_Cell_Header
{
    UILabel *_title;
    UILabel *_courierName;
    UIImageView *_rightArrow;
    BOOL _canOpen;
}

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    //    self.topLine.hidden = YES;
    self.contentView.backgroundColor = [UIColor whiteColor];
    
    _title = [[UILabel alloc] init];
    _title.textColor = kOrderBlack1;
    _title.backgroundColor = [UIColor clearColor];
    _title.numberOfLines = 1;
    _title.font = UIFontCustom(15);
    _title.text = kOrderLocalizeString(@"BKOrderChooseCourier");
    [self.contentView addSubview:_title];
    
    _courierName = [[UILabel alloc] init];
    _courierName.textColor = kOrderBlack1;
    _courierName.backgroundColor = [UIColor clearColor];
    _courierName.numberOfLines = 1;
    _courierName.font = UIFontCustom(14);
    [self.contentView addSubview:_courierName];
    
    _rightArrow = [[UIImageView alloc] init];
    [self.contentView addSubview:_rightArrow];
    
    [self makeConstraints];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    tap.numberOfTapsRequired = 1;
    [self.contentView addGestureRecognizer:tap];
}

- (void)makeConstraints
{
    [_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ApplicationBackGauge);
        make.centerY.equalTo(self);
    }];
    
    [_rightArrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-ApplicationBackGauge);
        make.centerY.equalTo(self);
    }];
    
    [_courierName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_rightArrow.mas_left).offset(-5);
        make.centerY.equalTo(self);
    }];
    
}

-(void)tap:(UIGestureRecognizer*)recognizer {
    
    if (!_canOpen) {
        return;
    }
    
    if(self.delegate && [self.delegate conformsToProtocol:@protocol(BK_Confirm_Order_ChooseExpress_HeadViewDelegate)]) {
        [self.delegate orderChooseCourierHeadViewDidSelected];
        
        [self doSomethingWithOpen];
    }
}

- (void)doSomethingWithOpen{
    CGAffineTransform transform;
    BOOL isHidden;
    if(CGAffineTransformEqualToTransform(_rightArrow.transform, CGAffineTransformIdentity)) {
        transform = CGAffineTransformMakeRotation(M_PI);
        isHidden = YES;
        //        self.bottomLine.hidden = isHidden;
    } else {
        transform = CGAffineTransformIdentity;
        isHidden = NO;
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        _rightArrow.transform = transform;
    } completion:^(BOOL finished) {
        //        self.bottomLine.hidden = isHidden;
    }];
}

- (void)fillData:(id)data{
    BKOrderCurrentCourier *cCourier = data;
    if (cCourier) {
        _courierName.text = cCourier.courier.expressCompanyName;
        
        if (cCourier.count <= 1) {
            [self updateConstraints_self];
            return;
        }
        _canOpen = YES;
        _rightArrow.image = [UIImage imageNamed:@"BKOrder_arrow_down"];
    }
}

- (void)updateConstraints_self{
    
    [_rightArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(-5);
        
    }];
    
    [_courierName mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-ApplicationBackGauge);
        make.centerY.equalTo(self);
    }];
}


+ (CGFloat)computeHeight:(id)data{
    return 45;
}

- (void)changeCourierName:(NSString *)courierName{
    _courierName.text = courierName;
    [self doSomethingWithOpen];
}

@end






















